package com.android.filter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.android.filter.FrameStatus.ChangeStatusCallback;
import com.example.plasma.R;
public class Frame {
	interface FrameInterface
	{
		void destroy();
		void configurationChanged();
		void backPressed();
		void setChangeStatusCallback(ChangeStatusCallback callback);
	}
	public Frame(Context context)
	{
		mContext = context;
	}
	public void setMosaicViewNUri(Uri uri)
	{
		if(mFrameInterface instanceof Mosaic)
			((Mosaic)mFrameInterface).setMosaicViewNUri(uri);
	}
	public boolean onCreateOptionMenu(Menu menu, MenuInflater inflater)
	{
		if(mFrameInterface instanceof Mosaic)
			inflater.inflate(R.menu.action_bar_menu, menu);
		return true;
	}
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		return true;
	}
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if(mFrameInterface instanceof Mosaic)
		{
			switch (item.getItemId()) {
			case R.id.menu_save:
				((Mosaic)mFrameInterface).save();
				break;

			case R.id.menu_share:
				break;
			}
		}
		return true;
	}
	public void changeStatus(int status)
	{
		if(mFrameInterface != null)
			mFrameInterface.destroy();
		if(status == LAUNCHER)
		{
			((Activity)mContext).invalidateOptionsMenu();
			LinearLayout layout = (LinearLayout)((Activity)mContext).findViewById(R.id.main_layout);
			layout.removeAllViews();
			LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
			inflater.inflate(R.layout.launcher_layout, layout);
			
			mFrameInterface = new Launcher(mContext);
			mFrameInterface.setChangeStatusCallback(new ChangeStatusCallback() {
				
				public void changeStatus(int status) {
					Frame.this.changeStatus(status);
				}
			});
		}
		else if(status == MOSAIC)
		{
			((Activity)mContext).invalidateOptionsMenu();
			LinearLayout layout = (LinearLayout)((Activity)mContext).findViewById(R.id.main_layout);
			layout.removeAllViews();
			LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
			inflater.inflate(R.layout.mosaic_layout, layout);
			
			mFrameInterface = new Mosaic(mContext);
			mFrameInterface.setChangeStatusCallback(new ChangeStatusCallback() {
				
				public void changeStatus(int status) {
					Frame.this.changeStatus(status);
				}
			});
		}
		else if(status == KILL)
		{
			((Activity)mContext).finish();
		}
//		mFrameInterface.initButtons();
//		mFrameInterface.initDrawView();
	}
	public void destroy()
	{
		mFrameInterface.destroy();
	}
	public void configurationChanged()
	{
		mFrameInterface.configurationChanged();
	}
	public void backPressed()
	{
		mFrameInterface.backPressed();
	}
	private Context mContext = null;
	private FrameInterface mFrameInterface = null;
	static final int LAUNCHER 	= 0x00000001;
	static final int MOSAIC		= 0x00000002;
	static final int KILL		= 0x00000003;
}
